//
//  UITextField+Extension.swift
//  VISUAL
//
//  Created by 刘森 on 2019/12/10.
//  Copyright © 2019 admin. All rights reserved.
//

import Foundation
import UIKit


private var maxLengthKey = 100
private var UITextFieldInputStyleKey = 101

enum UITextFieldInputStyle{
    case none
    case number
    case numberAndLetter
    case space
}

extension UITextField  {
    
    
    
    /**
     * 配合代理使用 UITextFieldDelegate
     * func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
     *     return  string == "" ? true : textField.text?.count ?? 0 < textField.maxLength
     * }
     */
    var maxLength: Int {
        set {
            objc_setAssociatedObject(self, &maxLengthKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC)
        }
        get {
            if let rs = objc_getAssociatedObject(self, &maxLengthKey) as? Int {
                return rs
            }
            return 10000
        }
    }
    
    var inputStyle: UITextFieldInputStyle {
        set {
            objc_setAssociatedObject(self, &UITextFieldInputStyleKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC)
        }
        get {
            if let rs = objc_getAssociatedObject(self, &UITextFieldInputStyleKey) as? UITextFieldInputStyle {
                return rs
            }
            return .none
        }
    }
}



